#!/usr/bin/python
from __future__ import generators
import operator,sys,math
from random import *
from string import *

def selections(items, n):
    if n==0: yield []
    else:
        for i in items:
            for ss in selections(items, n-1):
                yield [i]+ss

def parityFun(inData):
	outList = []
	parity  = 0
	#Calcolo di parita'. Il bit meno significativo e' quello di parita'
	for dataEl in inData:
		if  dataEl== '1':
			parity = 1 - parity
		outList.append('*')
	
	
	outList.pop()
	outList.append(str(parity))
	return outList

def invertFun(inData):
	outList = []
	inData= inData[::-1]
	for dataEl in inData:
		outList.append(dataEl)
	
	return outList
	
def allOne(inData):
	outList = []
	for dataEl in inData:
		outList.append('1')
	
	return outList
	
def BitSum(inData1,inData2):
    
    Val1 = int("".join(inData1),2)
    Val2 = int("".join(inData2),2)    
    
    Res  = bin(Val1+Val2)[2:]
    return list('0'*(len(inData1)-len(Res)) + Res)
    #return list(Res)
    
def CreaTrainingSet2(filePath,n_Input, fun):
    
    file = open(filePath,"w")
    bitLen = n_Input / 2
    bitPadOut	= ["*"]* (8-n_Input+2)
    for val in selections('01',n_Input):
    
        inData1 = ['0']*(4-bitLen) + val[0:bitLen]
        inData2 = ['0']*(4-bitLen) + val[bitLen:2*bitLen]
        
        val = inData1 + inData2
        
        res = fun(inData1,inData2)
        res = bitPadOut + res
        print val,
        print res 
        file.write("[" + ",".join(val) + "]>[" + ",".join(res) + "]\n")
        
    file.close()

def sum2(inData):
    bitLen = len(inData)/2
    
    inData1 = ['0']*(4-bitLen) + inData[0:bitLen]
    inData2 = ['0']*(4-bitLen) + inData[bitLen:2*bitLen]
    
    Val1 = int("".join(inData1),2)
    Val2 = int("".join(inData2),2) 
    
    Res = bin(Val1+Val2)[2:]
    return list('0'*(3-len(Res)) + Res)
    
def CreaTrainingSet(filePath, n_Input, fun, n_BitPad = 0):
	file = open(filePath,"w")
	bitPad		= ["0"]*n_BitPad
	bitPadOut	= ["*"]* (n_BitPad)
	for val in selections('01',n_Input):
		
		res = fun(val)
		val = bitPad + val
		res = bitPadOut + res
		print val,
		print res 
		file.write("[" + ",".join(val) + "]>[" + ",".join(res) + "]\n")
	
	file.close()

def CreaConter(filePath,n_Input,bitConter):
    file = open(filePath, 'w')
    n_BitPad = n_Input - bitConter
    bitPadOut	= ["*"]* (n_BitPad)
    inVal = ['0']*n_Input
    for val in selections('01', bitConter):
        val = bitPadOut + val
        print inVal,
        print val 
        file.write("[" + ",".join(inVal) + "]>[" + ",".join(val) + "]\n")
        
    file.close()

def main():
	#CreaTrainingSet("trainSet20.txt",8,lambda x: x[:])
    CreaConter('trainSet23.txt',8,4)
    			
if __name__ == "__main__":
	main()
	